from typing import Dict, List, Any, Optional, Callable

# Simple tool registry to describe available actions and how they map to executor

class Tool:
    def __init__(self, name: str, description: str, schema: Dict[str, Any]):
        self.name = name
        self.description = description
        self.schema = schema  # JSON schema-like hint for LLM

    def hint_line(self) -> str:
        # A concise hint line for prompts
        return f"- {self.name}: {self.schema} // {self.description}"


class ToolRegistry:
    def __init__(self):
        self._tools: Dict[str, Tool] = {}

    def register(self, name: str, description: str, schema: Dict[str, Any]) -> None:
        self._tools[name] = Tool(name, description, schema)

    def list(self) -> List[Tool]:
        return list(self._tools.values())

    def names(self) -> List[str]:
        return list(self._tools.keys())

    def prompt_hints(self) -> List[str]:
        return [t.hint_line() for t in self._tools.values()]


# Default registry for this app. Maps to ActionExecutor's action types

def build_default_registry() -> ToolRegistry:
    reg = ToolRegistry()
    # Web
    reg.register(
        name="web_search",
        description="在浏览器中搜索关键词（使用配置的搜索引擎）",
        schema={"type": "web_search", "query": "string"},
    )
    reg.register(
        name="navigate",
        description="打开浏览器导航到指定URL或域名",
        schema={"type": "navigate", "url": "string"},
    )
    reg.register(
        name="browser_click",
        description="在当前打开的搜索结果或页面上点击链接",
        schema={"type": "browser_click", "target": "first_result|link_text", "value": "string?"},
    )
    # Apps
    reg.register(
        name="app_control",
        description="打开/关闭/登录常见Windows应用（映射别名，如微信、浏览器等）",
        schema={"type": "app_control", "op": "open|close|login", "app": "string"},
    )
    # 新增：即时通讯消息发送
    reg.register(
        name="wechat_message",
        description="在微信中向指定联系人发送文本消息",
        schema={"type": "wechat_message", "contact": "string", "text": "string"},
    )
    reg.register(
        name="qq_message",
        description="在QQ中向指定联系人发送文本消息",
        schema={"type": "qq_message", "contact": "string", "text": "string"},
    )
    # System
    reg.register(
        name="system_control",
        description="调整系统设置（音量、静音、窗口置顶/聚焦等）",
        schema={"type": "system_control", "op": "set_volume|mute|unmute|increase_volume|decrease_volume|focus_window", "value": "number?", "target": "string?"},
    )
    # Files
    reg.register(
        name="file_op",
        description="执行基本文件操作（移动、复制、重命名、列出目录、打开文件）",
        schema={"type": "file_op", "op": "move|copy|rename|list|open", "src": "string", "dest": "string?", "name": "string?"},
    )
    # Content generation
    reg.register(
        name="write_article",
        description="写一篇文章并保存到指定目录",
        schema={"type": "write_article", "topic": "string", "words": "number", "save_path": "string"},
    )
    # Media
    reg.register(
        name="play_music",
        description="播放本地或在线音乐（尽力匹配本地文件名）",
        schema={"type": "play_music", "song_name": "string"},
    )
    # Clarification
    reg.register(
        name="needs_clarification",
        description="当指令不明确时，提出一个澄清问题",
        schema={"type": "needs_clarification", "question": "string"},
    )
    return reg